const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    address_list: [],
    is_address_picker: 0,
    cur_address: 0,
    showcopyarea: false,
    content: '',
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-21.png',
      text: '暂无收货地址哦~',
    },
    imgUrl: app.imgUrl,
    isBtn: true,
  },

  onLoad(options) {
    let opts = options || {};
    let is_address_picker = options.picker || 0;
    let cur_address = options.addr_id || 0;
    let phone = wx.getSystemInfoSync();  //调用方法获取机型
    let phoneType;
    if (phone.platform == 'ios') {
      phoneType = true;
    } else if (phone.platform == 'android') {
      phoneType = false;
    }
    this.setData({
      is_address_picker,
      cur_address,
      opts,
      phoneType,
    });
    app.pageOnLoad(this);
  },

  onShow() {
    let _this = this;
    let is_address_picker = this.data.is_address_picker || 0;
    let cur_address = Number(this.data.cur_address) || 0;
    wx.showNavigationBarLoading();
    app.ws_micro_shoprequest({
      url: api.user.address_list,
      success: function(res) {
        wx.hideNavigationBarLoading();
        if (res.code === 0) {
          let { list: address_list, address_switch, dispatch_type } = res.data;
          if (is_address_picker && address_list.length > 0) {
            for (let idx in address_list) {
              if (Object.prototype.hasOwnProperty.call(address_list, idx)) {
                Number(address_list[idx].id) === cur_address
                  ? (address_list[idx].is_used = 1)
                  : (address_list[idx].is_used = 0);
              }
            }
            // _this.defaultAddressChoose(address_list);
          }
          _this.setData({
            address_switch,
            dispatch_type,
            address_list,
          });
        }
        _this.setData({
          show_no_data_tip: _this.data.address_list.length === 0,
        });
      },
    });
  },

  setDefaultAddress(e) {
    let _this = this;
    let index = Number(e.currentTarget.dataset.index);
    let address = _this.data.address_list[index];
    wx.showLoading({
      title: '正在保存',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.address_set_default,
      data: {
        address_id: address.id,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let address_list = _this.data.address_list;
          for (let i in address_list) {
            if (Object.prototype.hasOwnProperty.call(address_list, i)) {
              Number(i) === index
                ? (address_list[i].is_default = 1)
                : (address_list[i].is_default = 0);
            }
          }
          _this.setData({
            address_list,
          });
        }
      },
    });
  },

  deleteAddress(e) {
    let address_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除该收货地址？',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '正在删除',
            mask: true,
          });
          app.ws_micro_shoprequest({
            url: api.user.address_delete,
            data: {
              address_id,
            },
            success: function(res) {
              if (res.code === 0) {
                wx.redirectTo({
                  url: '/ws_micro_shop/pages/address/address',
                });
              }
              if (res.code === 1) {
                wx.hideLoading();
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                });
              }
            },
          });
        }
      },
    });
  },

  defaultAddressChoose(list) {
    if (this.data.is_address_picker) {
      for (let i in list) {
        if (Number(list[i].is_used) === 1) {
          wx.setStorageSync('picker_address', list[i]);
        }
      }
    }
  },

  toUseAddress(e) {
    let index = e.currentTarget.dataset.index;
    let { is_address_picker, address_list } = this.data;
    let address = address_list[index];
    wx.setStorageSync('picker_address', address);
    Number(is_address_picker) === 1 && wx.navigateBack();
  },

  showCopyArea() {
    this.setData({
      showcopyarea: true,
    });
  },

  hideCopyArea() {
    this.setData({
      showcopyarea: false,
      content: '',
    });
  },

  capture() {
    // do nothing, but it`s necessary
  },

  getInputText(e) {
    this.setData({
      content: e.detail.value,
    });
  },

  toAddAddress() {
    wx.navigateTo({
      url:
        '/ws_micro_shop/pages/address-edit/address-edit?address_info=' +
        this.data.content +
        '&address_switch=' +
        this.data.address_switch +
        '&dispatch_type=' +
        this.data.dispatch_type,
    });
    this.setData({
      showcopyarea: false,
      content: '',
    });
  },

  getWechatAddress() {
    let that = this;
    that.setData({
      isBtn: false
    })
    wx.getSetting({
      success(res) {
        if (typeof res.authSetting['scope.address'] === 'undefined') {
          that.chooseAddress();
        } else if (res.authSetting['scope.address'] === false) {
          that.setData({
            open_setting: true,
          });
        } else {
          that.chooseAddress();
        }
        let inter = setTimeout(()=> {
          that.setData({
            isBtn: true
          })
          clearTimeout(inter);
        }, 2000)
      },
    });
  },

  chooseAddress() {
    wx.chooseAddress({
      success: function(e) {
        if (e.errMsg !== 'chooseAddress:ok') {
          return;
        }
        app.util.navigateTo(
          '/ws_micro_shop/pages/address-edit/address-edit',
          { addrInfo: JSON.stringify(e) },
          false
        );
      },
    });
  },
});
